package com.turing.post.cardmanagement.impl;

import java.math.BigInteger;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.cardmanagement.util.SalesGetCardOrder;
import com.turing.post.cardmanagement.util.SalesGetCardOrderDao;
import com.turing.post.util.Config;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

/**
 * 临时领卡\缴回单 Dao接口实现
 * 
 * @author kangzhe
 * 
 */
@Stateless(mappedName = "cardmanagement.impl.SalesGetCardOrderDaoImpl")
@Remote
@Local
public class SalesGetCardOrderDaoImpl implements SalesGetCardOrderDao {

	/**
	 * 实体管理器
	 * 
	 * @see javax.persistence.EntityManager
	 */
	@PersistenceContext
	protected EntityManager _em;

	@Override
	public void add(SalesGetCardOrder sgco) throws PCPSApplicationException {
		GeneralDao.insert(_em, sgco);
	}

	@Override
	public void update(SalesGetCardOrder sgco) throws PCPSApplicationException {
		GeneralDao.update(_em, sgco);
	}

	@Override
	public SalesGetCardOrder getInfoByOrderId(BigInteger salesGetCardOrderId) {
		return _em.find(SalesGetCardOrder.class, salesGetCardOrderId);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SalesGetCardOrder> getSalesGetCardOrderByConditions(
			PCPSWebInput input, String salesGetCardOrderNo,
			String saleBranchNo, String startTranDate, String endTranDate,
			String orderType, Integer page) {
		int maxResult = Config.PAGE_SIZE;
		int startPosition = maxResult * page;
		String brchSql = PCPSUtil.getSqlCond(input, saleBranchNo,
				"SaleBranchNo");
		String sql = "select * from salesgetcardorder t "
				+ " where (?1 is null or t.salesgetcardorderno = ?1) "
				+ brchSql
				+ " and (t.trantime >= to_date(?2,'yyyy-mm-dd hh24:mi:ss'))"
				+ " and (t.trantime <= to_date(?3,'yyyy-mm-dd hh24:mi:ss'))"
				+ " and (?4 is null or t.ordertype = ?4)"
				+ " order by t.trantime desc";
		Query query = _em.createNativeQuery(sql, SalesGetCardOrder.class);
		query.setParameter(1, salesGetCardOrderNo);
		query.setParameter(2, startTranDate + " 00:00:00");
		query.setParameter(3, endTranDate + " 23:59:59");
		query.setParameter(4, orderType);
		query.setFirstResult(startPosition);
		query.setMaxResults(maxResult);
		return query.getResultList();
	}

	@Override
	public BigInteger getSalesGetCardOrderTotalByConditions(PCPSWebInput input,
			String salesGetCardOrderNo, String saleBranchNo,
			String startTranDate, String endTranDate, String orderType) {
		String brchSql = PCPSUtil.getSqlCond(input, saleBranchNo,
				"SaleBranchNo");
		String sql = "select nvl(count(*),0) from salesgetcardorder t "
				+ " where (?1 is null or t.salesgetcardorderno = ?1) "
				+ brchSql
				+ " and (t.trantime >= to_date(?2,'yyyy-mm-dd hh24:mi:ss'))"
				+ " and (t.trantime <= to_date(?3,'yyyy-mm-dd hh24:mi:ss'))"
				+ " and (?4 is null or t.ordertype = ?4)";
		Query query = _em.createNativeQuery(sql);
		query.setParameter(1, salesGetCardOrderNo);
		query.setParameter(2, startTranDate + " 00:00:00");
		query.setParameter(3, endTranDate + " 23:59:59");
		query.setParameter(4, orderType);
		return new BigInteger(query.getSingleResult().toString());
	}

	@SuppressWarnings("unchecked")
	@Override
	public SalesGetCardOrder getSalesGetCardOrderBySalesGetCardOrderNo(
			String salesGetCardOrderNo) {
		String sql = "select * from SalesGetCardOrder where SalesGetCardOrderNo=?1";
		Query query = _em.createNativeQuery(sql, SalesGetCardOrder.class);
		query.setParameter(1, salesGetCardOrderNo);
		List<SalesGetCardOrder> list = query.getResultList();
		if (list == null && list.size() < 1) {
			return null;
		}
		return list.get(0);
	}
}
